package com.haoxi.gateway.cngpmsg;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.util.TypeConvert;

public class CNGPSubmitMessage extends CNGPMessage {

    private StringBuffer strBuf;

    public CNGPSubmitMessage(String SPId, int subType, int needReport,
            int priority, String serviceId, String feeType, int feeUserType,
            String feeCode, int msgFormat, Date validTime, Date atTime,
            String srcTermId, String chargeTermId, int destTermIdCount,
            String destTermId[],  String msgContent,
            int protocolValue) throws IllegalArgumentException {
        if (SPId == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:SPId value is null.");
        if (SPId.length() > 10)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:SPId length is larger than 10");
        if (subType < 0 || subType > 3)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:subType less than 0 or larger than 3.");
        if (needReport < 0 || needReport > 1)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:needReport less than 0 or larger than 1.");
        if (priority < 0 || priority > 3)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:priority less than 0 or larger than 3.");
        if (serviceId == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:serviceId value is null.");
        if (serviceId.length() > 10)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:serviceId less than 0 or larger than 10.");
        if (feeType == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:feeType value is null.");
        if (feeType.length() > 2)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:feeType length is larger than 2");
        if (feeUserType < 0 || feeUserType > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:feeUserType less than 0 or larger than 255.");
        if (feeCode == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:feeCode value is null.");
        if (feeCode.length() > 6)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:feeCode length is larger than 6");
        if (msgFormat < 0 || msgFormat > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:msgFormat than 0 or larger than 255.");
        if (srcTermId == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:srcTermId value is null.");
        if (srcTermId.length() > 21)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:srcTermId length is larger than 21");
        if (chargeTermId == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:chargeTermId value is null.");
        if (chargeTermId.length() > 21)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:chargeTermId length is larger than 21");
        if (destTermIdCount < 0 || destTermIdCount > 100)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:destTermIdCount than 0 or larger than 100.");
        if (destTermId == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:destTermId value is null.");
//        if (msgLength < 0 || msgLength > 255)
//            throw new IllegalArgumentException(
//                    "The parameter of submit message is wrong:msgLength 0 or larger than 255.");
        if (msgContent == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:msgContent value is null.");
        if (msgContent.length() > 254)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:msgContent length is larger than 254");
        if (protocolValue < 0 || protocolValue > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:protocolValue 0 or larger than 255.");
        byte ms[] = null;
        try {
            ms = msgContent.getBytes("gb2312");
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        int len = 132 + 21 * destTermIdCount + ms.length;
        buf = new byte[len];
        setMsgLength(len);
        setRequestId(2);
        System.arraycopy(SPId.getBytes(), 0, buf, 16, SPId.length());
        buf[26] = (byte) subType;
        buf[27] = (byte) needReport;
        buf[28] = (byte) priority;
        System.arraycopy(serviceId.getBytes(), 0, buf, 29, serviceId.length());
        System.arraycopy(feeType.getBytes(), 0, buf, 39, feeType.length());
        buf[41] = (byte) feeUserType;
        System.arraycopy(feeCode.getBytes(), 0, buf, 42, feeCode.length());
        buf[48] = (byte) msgFormat;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        if (validTime != null) {
            String tmpTime = dateFormat.format(validTime).concat("032+");
            System.arraycopy(tmpTime.getBytes(), 0, buf, 49, 16);
        }
        if (atTime != null) {
            String tmpTime = dateFormat.format(atTime).concat("032+");
            System.arraycopy(tmpTime.getBytes(), 0, buf, 66, 16);
        }
        System.arraycopy(srcTermId.getBytes(), 0, buf, 83, srcTermId.length());
        System.arraycopy(chargeTermId.getBytes(), 0, buf, 104, chargeTermId
                .length());
        buf[125] = (byte) destTermIdCount;
        int i = 0;
        for (i = 0; i < destTermIdCount; i++)
            System.arraycopy(destTermId[i].getBytes(), 0, buf, 126 + i * 21,
                    destTermId[i].length());

        int pos = 126 + 21 * destTermIdCount;
        buf[pos] = (byte) ms.length;
        pos++;
        System.arraycopy(ms, 0, buf, pos, ms.length);
        pos += ms.length;
        TypeConvert.short2byte(256, buf, pos);
        pos += 2;
        TypeConvert.short2byte(1, buf, pos);
        pos += 2;
        buf[pos] = (byte) protocolValue;
        strBuf = new StringBuffer();
        strBuf.append(",SPId=").append(SPId);
        strBuf.append(",SubType=").append(subType);
        strBuf.append(",NeedReport=").append(needReport);
        strBuf.append(",Priority=").append(priority);
        strBuf.append(",ServiceID=").append(serviceId);
        strBuf.append(",FeeType=").append(feeType);
        strBuf.append(",FeeUserType=").append(feeUserType);
        strBuf.append(",FeeCode=").append(feeCode);
        strBuf.append(",MsgFormat=").append(msgFormat);
        strBuf.append(",ValidTime=").append(validTime);
        strBuf.append(",AtTime=").append(atTime);
        strBuf.append(",SrcTermID=").append(srcTermId);
        strBuf.append(",ChargeTermID=").append(chargeTermId);
        strBuf.append(",DestTermIDCount=").append(destTermIdCount);
        for (int t = 0; t < destTermIdCount; t++)
            strBuf.append(",DestTermID[").append(t).append("]=").append(
                    destTermId[t]);

        strBuf.append(",MsgLength=").append(ms.length);
        strBuf.append(",MsgContent=").append(msgContent);
        strBuf.append(",ProtocolValue=").append(protocolValue);
    }

    public String toString() {
        StringBuffer outBuf = new StringBuffer("CNGPSubmitMessage: ");
        outBuf.append("PacketLength=").append(getMsgLength());
        outBuf.append(",RequestID=").append(getRequestId());
        outBuf.append(",Status=").append(getStatus());
        outBuf.append(",SequenceID=").append(getSequenceId());
        if (strBuf != null)
            outBuf.append(strBuf.toString());
        return outBuf.toString();
    }
}
